<?php

    global $mikron;

    $registration = new registration_class();

    // добавление необходимых сущностей
    $mikron->Schema->Add(dirname(__FILE__).'/schema.xml', 'Схема модуля Registration');
    $mikron->OperatingInstructions->AddFunction('MIKRON_REGISTRATION', 'OperatingInstruction_ShowRegistrationForm', $registration);
    $mikron->Handlers->AddSaveHandler('SavingREGINFO', Array('T_REGINFO'), $registration);

    class registration_class
    {
        public $registered = false;
        public $registerForm = null;

        function OperatingInstruction_ShowRegistrationForm($parameters)
        {
           global $mikron;
           if(is_bool($mikron->Cache->getCache('registered')))
           {
               $mikron->Cache->setCache('registered', null);
               $mikron->Tools->GoToPage($mikron->Tools->ClearURI($_SERVER['REQUEST_URI']).'?ok');
           }
           else
           {
               if(isset($_GET['ok']))
               {
                    echo '{{nicewindow style="silver" caption="Поздравляем!" width="320"}}
                    Вы успешно зарегистрированы.
                    Теперь можете войти в свой аккаунт.{{/nicewindow}}
                    {{MIKRON_USERFORM}}';
               }
               else
               {
                   if($this->registerForm instanceof T_REGINFO)
                   {
                       $rinf = $this->registerForm;
                   }
                   else
                   {
                       $rinf = new T_REGINFO();
                   }
                   $mikron->EntityManager->ShowFormForUsers($rinf, 'Регистрация в системе',
                   false, true, $_SERVER['REQUEST_URI'], false,
                   $_SERVER['REQUEST_URI'], null, null, null, null, null, 'Зарегистрироваться', 1);
               }
           }
        }

        /**
        * Handler.
        * 
        * @param T_REGINFO $result
        */
        function SavingREGINFO($result)
        {
            try
            {
                if($result instanceof MikronException)
                {
                    throw new Exception($result->Description, $result->Code);
                }
                elseif(is_null($result))
                {
                    throw new Exception('Неизвестная ошибка, попробуйте еще раз.');
                }
                else
                {
                    global $mikron;
                    if(!$result->Validate())
                    {
                        $this->registerForm = $result;
                        return;
                    }
                    foreach($mikron->Handlers->Handlers(HANDLER_REGISTRATION) as $handler)
                    {
                        $object = $handler->object;
                        $function = $handler->processor;
                        if(!$object->$function($result))
                        {
                            return $this->Registered();
                        }
                    }
                    /**
                    * warning: Need create T_USER
                    */
                    $this->Registered();
                }
            }
            catch(Exception $ex)
            {
                if($ex->getCode() != 0)
                {
                    echo '{{nicewindow style="error" caption=""
                    width="320"}}<h2 style="margin: 0px;">Внимание!</h2>'.$ex->getMessage().'{{/nicewindow}}';
                }
                $this->registerForm = $result;
            }
            return false;
        }
        
        private function Registered()
        {
            global $mikron;
            $mikron->Cache->setCache('registered', true);
            $this->registered = true;            
        }
    }

?>